﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Web.View.CustomerView
{
    public partial class CustomerList : System.Web.UI.Page
    {

        #region Public Properties

        public List<int> EditCustomers
        {
            get
            {
                if (ViewState["_editcustomers"] != null)
                    return (List<int>)ViewState["_editcustomers"];
                else
                    return null;
            }
            set
            {
                ViewState["_editcustomers"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            if (Page.IsPostBack != true)
            {
                
            }

            // Bind customer data to gridview
            BindCustomerData(db);

            // Change gridview pagesize based on dropdownlist
            gvCustomer.SettingsPager.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);

            // Dispose connection to database
            db.Dispose();
        }

        /// <summary>
        /// Function that bind customer data to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        private void BindCustomerData(TireServiceDB db)
        {
            gvCustomer.DataSource = db.GetCustomersView();
            gvCustomer.DataBind();
        }

        #region Private Functions

        private void EditSelectedCustomers()
        {
            throw new ArgumentException("Funktion ikke implementeret endnu.");
        }

        /// <summary>
        /// Function that change grouping in order gridview
        /// </summary>
        /// <param name="ViewIndex">Integer setting the grouping mode</param>
        protected void GroupOrderData(int ViewIndex)
        {
            gvCustomer.ClearSort();
            gvCustomer.GroupSummary.Clear();
            switch (ViewIndex)
            {
                case 0:
                    // Nothing - clear grouping

                    break;
                case 1:
                    gvCustomer.GroupBy(gvCustomer.Columns["ZipCode"]);

                    break;
            }
            gvCustomer.ExpandAll();
        }

        #endregion

        #region Control Functions

        /// <summary>
        /// Button function for creating a new customerList
        /// </summary>
        protected void btnNewCustomer_Click(object sender, EventArgs e)
        {
            // Go to edit mode
            Session["CustomerId"] = null;
            Response.Redirect("~/View/CustomerView/EditCustomerData.aspx");
        }

        /// <summary>
        /// Button function for export of gridview
        /// </summary>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (ddlExport.Value == "0")
            {
                gvExport.WritePdfToResponse();
            }

            if (ddlExport.Value == "1")
            {
                gvExport.WriteXlsToResponse();
            }

            if (ddlExport.Value == "2")
            {
                gvExport.WriteCsvToResponse();
            }

            if (ddlExport.Value == "3")
            {
                gvExport.WriteRtfToResponse();
            }
        }
        
        /// <summary>
        /// Dropdown list function that change gridview grouping when selected item is changed
        /// </summary>
        protected void ddlView_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Group data
            GroupOrderData(Convert.ToInt32(ddlView.SelectedValue));
        }

        #endregion

        #region GridView Functions

        /// <summary>
        /// Gridview function that change field data for unbound fields
        /// </summary>
        protected void gvCustomer_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
        {
            // Format UnBoundColumn Type: Show country name
            if (e.Column.FieldName == "CountryName")
            {
                // Get countryid
                int countryid = Convert.ToInt32(e.GetListSourceFieldValue("CountryId"));

                TireServiceDB db = new TireServiceDB();

                // Change field value
                e.Value = db.GetCountryName(countryid);

                db.Dispose();
            }
        }

        #endregion

    }
}